//
//  WorkHomeVC.m
//  XLDoctor
//
//  Created by longma on 2018/7/27.
//  Copyright © 2018年 XLH. All rights reserved.
//mj绑定电话号码

#import "BindPhoneNumberVC.h"
#import "LanguageController.h"
#import "CountryCodeVC.h"
#import "RegisterViewController.h"
#import "AllCountryCodeVC.h"
#import "NBPhoneNumberUtil.h"
@interface BindPhoneNumberVC ()
@property (weak, nonatomic) IBOutlet UILabel *countryCodeLabel;
@property (weak, nonatomic) IBOutlet UILabel *tipLabel;
@property (weak, nonatomic) IBOutlet MJUITextField *phoneNumberTextField;
@property (weak, nonatomic) IBOutlet MJUIBaseButton *confirmButton;
@property (weak, nonatomic) IBOutlet UIView *phoneNumberBgView;
@property (nonatomic, strong) NSDictionary *countryAreaCode;

@end

@implementation BindPhoneNumberVC

- (void)viewDidLoad {
    [super viewDidLoad];
 
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
}

- (void)mj_setUI{
    [super mj_setUI];
    self.mj_titleLocalizable = @"Bind Phone Number";

    self.phoneNumberTextField.mj_disableThirdPartyKeyboard = true;
//    self.phoneNumberTextField.font = MJUIFontBoldMake(20);
    
    self.tipLabel.hidden = true;
    self.countryCodeLabel.text = @"";
    [self.phoneNumberTextField mj_setClearButtonWithImage:[UIImage imageNamed:@"icon_del"]];
    [self.phoneNumberTextField mj_becomeFirstResponder];
    self.countryAreaCode = [HealthHelper getCountryAreaCode];
    NSString *code = [NSString stringWithFormat:@"+%@",self.countryAreaCode[@"区号"]];
    self.countryCodeLabel.text = code;
    
    if([self.countryCodeLabel.text containsString:@"+86"]){
        self.phoneNumberTextField.mj_limitedNumber = 11;
    }
    self.phoneNumberTextField.adjustsFontSizeToFitWidth = true;
    
    NBPhoneNumberUtil *phoneUtil = [NBPhoneNumberUtil sharedInstance];
    MJWeakSelf
    RAC(self.confirmButton, mj_enabled) = [RACSignal combineLatest:@[self.phoneNumberTextField.rac_textSignal] reduce:^id _Nullable(NSString * text){
        
        BOOL enable = false;
        if([self.countryCodeLabel.text containsString:@"+86"]){
            if(text.length == 11 && [text mj_isMobileNumber]){
                enable = true;
            }else{
                enable = false;
            }
        }else{
            NBPhoneNumber *myNumber = [phoneUtil parse:text
                                          defaultRegion:weakSelf.countryAreaCode[@"ISO"] error:nil];
            enable = [phoneUtil isValidNumber:myNumber];
            NSLog(@"isValidPhoneNumber ? [%@]", [phoneUtil isValidNumber:myNumber] ? @"YES":@"NO");
        }
      
        if(enable){
            [weakSelf.phoneNumberBgView mj_borderColor:UIColor.mj_appColor width:2];
        }else{
            [weakSelf.phoneNumberBgView mj_borderColor:UIColor.clearColor width:2];
            weakSelf.tipLabel.hidden = true;
        }
        return @(enable);
    }];
    

}

#pragma mark - 方法
- (void)requestData{
    
}

- (IBAction)handleCountryCodeButtonClick:(UIButton *)sender {
    MJWeakSelf
    AllCountryCodeVC *vc = [[AllCountryCodeVC alloc]init];
    vc.handleCountryCodeBlock = ^(NSDictionary *countryAreaCode) {
        NSString *code = [NSString stringWithFormat:@"+%@",countryAreaCode[@"区号"]];
        weakSelf.countryCodeLabel.text = code;
        weakSelf.countryAreaCode = countryAreaCode;

    };
    [self.navigationController pushViewController:vc animated:true];
}

- (IBAction)handleConfirmButtonClick:(UIButton *)sender {
    
    if(self.handleCountryAreaCodeBlock){
        self.handleCountryAreaCodeBlock(self.countryAreaCode,self.phoneNumberTextField.text);
        [self mj_popViewController];
    }

}

#pragma mark - 代理

#pragma mark - 懒加载

@end
